package com.weifangfirecontrolformal.weifangfirecontrolformal.web.service.internetofthings.firefightingiot;

import cn.hutool.json.JSONObject;
import com.weifangfirecontrolformal.weifangfirecontrolformal.common.base.BaseService;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.file.FileStrUtil;
import com.weifangfirecontrolformal.weifangfirecontrolformal.support.utils.requestutil.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

/**
 * @author Yang yu
 * @version 1.0
 * @date 2020/7/28 11:30
 */
@Service
@Slf4j
public class ReportService extends BaseService {


    /**
     * 报表导出
     * @param
     * @return
     */
    public ResponseEntity<byte[]> getReport(JSONObject jsonParam) throws Exception {
        jsonParam = HttpRequestUtil.commonHttpRequestParamConvert(getRequest());
        String filepath = (String) fireFightingiotJava(jsonParam, "getReport", "report").getData();
        byte[] btImg = FileStrUtil.getImageFromNetByUrl(filepath);
        HttpHeaders headers = new HttpHeaders();
        // 下载显示的文件名，解决中文名称乱码问题
        String downloadFielName = new String(filepath.substring(filepath.lastIndexOf("/")+1,filepath.length()).getBytes("UTF-8"), "iso-8859-1");
        // 通知浏览器以attachment（下载方式）打开文件
        headers.setContentDispositionFormData("attachment", downloadFielName);
        // application/octet-stream ： 二进制流数据（最常见的文件下载）。
        headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
        return new ResponseEntity<byte[]>(btImg, headers, HttpStatus.CREATED);
    }
}
